<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Task;
use App\Models\WeChatAccount;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;

class TaskController extends AdminController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header('任务列表')
            ->description('全部')
            ->breadcrumb(['text'=>'列表','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Task(['account']), function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('account_id');
            $grid->column('account.account','微信账号');
            $grid->column('query_name');
            $grid->column('match_name');
            $grid->column('replys')->display(function ($replys){
                return $replys ? $replys[0] : '';
            });
            $grid->column('status')->radio(\App\Models\Task::STATUS_MAP);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            // $grid->setActionClass(Grid\Displayers\Actions::class); // 行操作按钮显示方式 图标方式
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                 $actions->disableDelete(); //  禁用删除
//                 $actions->disableEdit();   //  禁用修改
                // $actions->disableQuickEdit(); //禁用快速修改(弹窗形式)
                 $actions->disableView(); //  禁用查看
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Task(), function (Show $show) {
            $show->field('id');
            $show->field('account_id');
            $show->field('query_name');
            $show->field('match_name');
            $show->field('replys');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Task(), function (Form $form) {
            $form->display('id');
            if($form->isEditing()){
                $form->display('account_id')->with(function ($value){
                    return WeChatAccount::where('id',$value)->value('account');
                });
            }else{
                $form->select('account_id')->options(WeChatAccount::getAccounts());
            }

            $form->text('query_name');
            $form->text('match_name');
            $form->list('replys')->max(20)->min(1);
            $form->radio('status')->options(\App\Models\Task::STATUS_MAP);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
